package org.dllwh.utils.apache.poi.word;


import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.dllwh.utils.apache.poi.word.conf.IndentUnit;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName WordParagraphHelper
 * @Description Word段落相关工具类
 * @Date 2025-10-24 13:52
 * @版本 V 1.0.1
 * @since JDK 17
 */
public final class WordParagraph {

    /**
     * 设置段落基本样式
     */
    public static void setParagraphTextBasicStyleInfo(XWPFParagraph paragraph, XWPFRun pRun) {

    }

    /**
     * 设置段落文本样式
     */
    public static void setParagraphTextSimpleStyleInfo(XWPFParagraph paragraph, XWPFRun pRun) {

    }

    /**
     * 设置段落字体信息
     */
    public static void setParagraphRunFontInfo(XWPFRun run, String content, String fontFamily, String fontSize) {
        if (StringUtils.isNotBlank(content)) {
            run.setText(content);
        }
        // 设置字体
        if (StringUtils.isNotBlank(fontFamily)) {
            run.setFontFamily(fontFamily);
        }

        // 设置字体大小
        if (StringUtils.isNotBlank(fontSize)) {
            run.setFontSize(Integer.parseInt(fontSize));
        }
    }

    /**
     * 设置段落对齐方式
     */
    public static void setParagraphAlignInfo(XWPFParagraph paragraph, ParagraphAlignment alignment, TextAlignment textAlignment) {
        if (alignment != null) {
            paragraph.setAlignment(alignment);
        }
        if (textAlignment != null) {
            paragraph.setVerticalAlignment(textAlignment);
        }
    }

    /**
     * 设置段落间距信息,一行=100 一磅=20
     *
     * @param paragraph    段落对象
     * @param spacesBefore 段前间距，行数
     * @param spacesAfter  段后间距，行数
     */
    public static void setParagraphTextInfo(XWPFParagraph paragraph, int spacesBefore, int spacesAfter) {
        if (spacesBefore > 0) {
            paragraph.setSpacingBefore(spacesBefore);
        }
        if (spacesAfter > 0) {
            paragraph.setSpacingAfter(spacesAfter);
        }

        /* 待验证

        CTPPr ppr = paragraph.getCTP().getPPr();
        if (ppr == null) {
            ppr = paragraph.getCTP().addNewPPr();
        }

        CTSpacing spacing = ppr.isSetSpacing() ? ppr.getSpacing() : ppr.addNewSpacing();
        spacing.setAfter(BigInteger.valueOf(spacesAfter));
        spacing.setBefore(BigInteger.valueOf(spacesBefore));
        */
    }

    /**
     * 设置段落缩进信息 1厘米≈567
     *
     * @param firstLineIndent 首行缩进
     * @param leftIndent      左缩进
     * @param rightIndent     右缩进
     * @param unit            单位
     */
    public static void setParagraphIndInfo(XWPFParagraph paragraph, int firstLineIndent, int leftIndent, int rightIndent, IndentUnit unit) {
        if (firstLineIndent > 0) {
            paragraph.setFirstLineIndent(firstLineIndent);
        }
        if (leftIndent > 0) {
            paragraph.setIndentationFirstLine(leftIndent);
        }
        if (rightIndent > 0) {
            paragraph.setIndentationHanging(rightIndent);
        }
    }

    /**
     * 设置段落边框信息
     */
    public static void setParagraphBorderInfo(XWPFParagraph paragraph) {

    }
}
